<!doctype html>
<html>
<head>
	<meta charset="utf-8">
	<title>设置</title>
	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
	<link href="../../css/mui.css" rel="stylesheet"/>
	<link href="../../css/mui.min.css" rel="stylesheet"/>
	<link href="../../css/app.css" rel="stylesheet"/>
</head>
<body>
	<!-- 头部TITLE -->
	<header class="mui-bar mui-bar-nav mui-bar-transparent">
		<span class="mui-icon mui-icon-back" onclick="mui.back()"></span>
		<h1 class="mui-title">设置</h1>
	</header>
	<!-- 头部TITLE -->
	
	<!-- 主体部分 -->
	<div style="height: 45px;"></div>
	<ul class="mui-table-view">
		<li class="mui-table-view-cell" onclick="jumpToMyAccount()">
			<a class="mui-navigate-right">
				我的账号
			</a>
		</li>
<!-- 		<li class="mui-table-view-cell">
			<a class="mui-navigate-right">
				 关于APP
			</a>
		</li> -->
		<li class="mui-table-view-cell">
			<a class="mui-navigate-right">
				 检查更新
				<span class="app-list-right" id="version"></span>
			</a>
		</li>
		<li class="mui-table-view-cell" onclick="jumpToMysetCache()">
			<a class="mui-navigate-right">
				 清理缓存
			</a>
		</li>
		<li class="mui-table-view-cell mui-hidden" id="logli"
			onclick="setLogout()">
			<a class="mui-navigate-right">
				 退出登陆
			</a>
		</li>
	</ul>
	<!-- 进度条 -->
	<div id="progressbar" class="mui-progressbar">
		<span></span>
	</div>
	<!-- 主体部分 -->
	
	<!-- 底部菜单 -->
	<nav class="mui-bar mui-bar-tab">
		<a class="mui-tab-item" onclick="jumpToIndex()">
			<span class="mui-icon mui-icon-home"></span>
			<span class="mui-tab-label">首页</span>
		</a>
		<a class="mui-tab-item" onclick="jumpToMarket()">
			<span class="mui-icon mui-icon-download"></span>
			<span class="mui-tab-label">库</span>
		</a>
		<a class="mui-tab-item" onclick="jumpToReward()">
			<span class="mui-icon mui-icon-spinner-cycle"></span>
			<span class="mui-tab-label">奖励</span>
		</a>
		<a class="mui-tab-item mui-active" onclick="jumpToMy()">
			<span class="mui-icon mui-icon-contact"></span>
			<span class="mui-tab-label">我的</span>
		</a>
	</nav>
	<!-- 底部菜单 -->
</body>
</html>

<script src="../../js/mui.js"></script>
<script src="../../js/app.js"></script>
<script src="../../js/views/view.js"></script>
<script type="text/javascript">
	// mui.init()
	//自动加载
	(function($, owner) {
		isLogin(); //判断登陆
		getAppVersion();
		// setDownloadTotal(1);
	}(mui, window.app = {}));
	// 判断是否登陆
	function isLogin(){
		var userInfo = cacheGet(userInfoKey);
		if (userInfo) {
			document.getElementById('logli').classList.remove('mui-hidden');
		} else {
			document.getElementById('logli').classList.add('mui-hidden');
			mui.toast('没有登陆',{ 
				duration:'short', type:'div',
			});
			setTimeout(function(){
				jumpToMyLogin();
			}, 500);
			return
		}
	}
	// APP版本
	function getAppVersion(){
		var appVersion = cacheGet(appVersionKey);
		var version = '0';  //版本号
		if (appVersion) { version = appVersion.version; }
		mui("#version")[0].innerHTML = 'v'+version;
		// 系统参数
		var sys = getSys();
		if (sys==null || sys.appAccess=='') { 
			setSys(); //参数更新
			setTimeout(function(){
				jumpToMyset();
			}, 1000);
		} 
		// 请求判断新版本
		mui.ajax(sys.domain+'/app/version/new',{
			data:{
				version:version
			},
			dataType:'json', //服务器返回json格式数据
			type:'post', //HTTP请求类型
			timeout:10000, //超时时间设置为10秒
			headers:{'Content-Type':'application/x-www-form-urlencoded'}, 
			success:function(res){
				// mui.toast('?'+res.code);
				// return
				if (res.code==-1) {
					mui.toast('需要更新系统参数',{ 
						duration:'long', type:'div',
					});
					setTimeout(function(){
						setSys();
						jumpToMyset();
					},500);
				} else if (res.code!=0) {
					var h = 'v'+version+'无新版本';
					mui("#version")[0].innerHTML = h;
					return
				}
				var ver = res.data.version;
				var link = res.data.link;
				// 需要下载新本本
				var h = '<span style="color:red;" onclick="setDownload()">v'+ver+'有新版本，点击获取<input type="hidden" id="appNewLink" value="'+link+'"/></span>';
				mui("#version")[0].innerHTML = h;
				//更新本地APP版本缓存
				var appVersion = {
					'version':ver,
					'link':link
				};
				cacheSet(appVersionKey,appVersion);
			},
			error:function(xhr,type,errorThrown){
				mui.toast('网络错误！',{ 
					duration:'short', type:'div',
				});
			}
		});
	}
	// APP下载安装
	function setDownload(){
		var link = mui("#appNewLink")[0].value;
		if (!link) { return }
		var version = cacheGet(appVersionKey);
		if (!version || !version.version) { return }
		var ver = version.version;
		setDownloadTotal(ver); //设置更新
		// 更新下载量
		// mui.toast(link); return
		mui.plusReady(function () {
			plus.nativeUI.toast("正在准备下载，请稍后！");
			var dtask = plus.downloader.createDownload(link, {}, function (d, status) {
				if (status == 200) {
					mui.toast('下载成功，准备安装！');
					var path = d.filename;
					plus.runtime.install(path); // 自动安装apk文件
					mui.toast('安装成功！');
				} else {
					plus.nativeUI.alert('版本更新失败:' + status);
				}
			});
			dtask.start();
			//下载安装进度显示
			dtask.addEventListener("statechanged", function(task, status) {
				switch(task.state) {
					case 1: // 开始
						mui.toast('开始！',{ 
							duration:'short', type:'div',
						});
						break;
					case 2: //已连接到服务器
						mui.toast('已连接到服务器！',{ 
							duration:'short', type:'div',
						});
						break;
					case 3: // 已接收到数据
						var dsize = task.downloadedSize;
						var tsize = task.totalSize;
						var per = dsize / tsize *100;
						per = parseInt(per);
						mui.toast(per+'%',{ 
							duration:'short', type:'div',
						});
						getProgressbar(per); //进度条
						break;
					case 4: // 下载完成 
						mui.toast('下载完成',{ 
							duration:'short', type:'div',
						});
						plus.runtime.install(task.filename, {}, function() {
								//成功安装回调，执行重启app操作
								plus.runtime.restart();
							}, function(DOMException) {
								mui.toast(JSON.stringify(DOMException));
								mui.toast("安装失败");
							});
						break;
				}
			});
		});
	}
	// 显示进度条
	function getProgressbar(per){
		mui('#progressbar').progressbar({progress:per}).show(); 
	}
	// 设置退出
	function setLogout(){
		var userInfo = cacheGet(userInfoKey);
		if (!userInfo) {
			mui.toast('没有登陆',{ 
				duration:'short', type:'div',
			});
			return
		}
		cacheDel(userInfoKey);
		mui.toast('退出成功',{ 
			duration:'short', type:'div',
		});
		setTimeout(function(){
			jumpToMyset();
		}, 500);
	}
	// 更新下载量
	function setDownloadTotal(){
		var version = cacheGet(appVersionKey);
		if (!version || !version.version) { return }
		// 系统参数
		var sys = getSys();
		// mui.toast(sys.domain)
		mui.ajax(sys.domain+'/app/version/total/modify',{
			data:{
				version:version.version
			},
			dataType:'json', //服务器返回json格式数据
			type:'post', //HTTP请求类型
			timeout:10000, //超时时间设置为10秒
			headers:{'Content-Type':'application/x-www-form-urlencoded'}, 
			success:function(res){
				mui.toast('res'+res.code)
				if (res.code==-1) {
					mui.toast('需要更新系统参数',{ 
						duration:'long', type:'div',
					});
					setTimeout(function(){
						setSys();
						jumpToMyset();
					},500);
				} else if (res.code!=0) {
					return
				}
			},
			error:function(xhr,type,errorThrown){
				mui.toast('网络错误！',{ 
					duration:'short', type:'div',
				});
			}
		});
	}
</script>